<?php

if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class skill_mapping extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->require_login();
		$this->load->model('skill_model', 'skill');
		$this->load->model('skill_mapping_model', 'mapping');
		$this->load->model('agent_model', 'agent');
		
	}

	function index() 
	{
		check_permission("skill_mapping", "view");
		
		$data = array();
		$data['page_heading'] = 'Skill Mapping';
		$GET = $this->input->get(NULL, true);
 		$this->config->set_item('enable_query_strings', TRUE);
		$config['base_url'] = base_url().$this->uri->segment(1).'/?';
		$config['per_page'] = '20';
 		$config['num_links'] = 5;	
		$start = $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$results = $this->mapping->getListingData($GET,$start);
		$data['start'] = $start;
		$data['results'] = $results['results'];
		$data['total_record'] = $results['total_record'];
		
		if(is_array(@$GET))	{
			unset($GET['per_page']);
 			$qs = http_build_query($GET); 
		} else {
			$qs = '';	
		}
		
		if(isset($qs) && !empty($qs)){
		   $this->pagination->suffix = "&".$qs;	
		}
		
		$config['total_rows'] = $data['total_record'];
		$config['full_tag_open'] = '<ul class="paging">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$perPage = $config['per_page'];
		$this->config->set_item('enable_query_strings', TRUE);
		$this->pagination->initialize($config);
	
		
		$this->load->view($this->uri->segment(1).'/listing', $data);
	}
	
	function save($id = 0) 
	{
		$id = (int)$id;
		
		if( $id > 0 ) {
			check_permission("skill_mapping", "edit");
		} else {
			check_permission("skill_mapping", "add");
		}
		
		$data = array();
		$data['all_skills'] = $this->skill->getAllskills();
		$data['page_heading'] = 'Skill Mapping';
		
		$this->form_validation->set_rules('primary_server_id', 'primary server', 'trim|required|xss_clean');
		$this->form_validation->set_rules('secondary_server_id', 'secondary server', 'trim|required|xss_clean');
		if( $id < 1) {
			$this->form_validation->set_rules('skill_id', 'skill', 'trim|required|callback_check_duplicate|xss_clean');
		}
		
		if ($this->form_validation->run() == FALSE )
		{
			if ( $id > 0)
			{
				$detail = $this->mapping->getDetailByID($id);
				$data['id']=$id;
				$data['skill_id'] = $detail['skill_id'];
				$data['primary_server_id']=$detail['primary_server_id'];
				$data['secondary_server_id']=$detail['secondary_server_id'];
				
			}
			$this->load->view($this->uri->segment(1).'/save', $data);
		} 
		else 
		{
			$record = array( 
								'skill_id' 				=> addslashes($this->input->post('skill_id')),
								'primary_server_id' 	=> addslashes($this->input->post('primary_server_id')),
								'secondary_server_id' 	=> addslashes($this->input->post('secondary_server_id'))
							);	
			
							
			if( $id > 0 ) // skill will not be updated
			{
				unset($record['skill_id']);
				$result=$this->mapping->update($record,$id);
				$this->session->set_flashdata('userMesg', 'Record has been updated successfully.');
			}
			else
			{
				$this->mapping->add($record);
				$this->session->set_flashdata('userMesg', 'Record has been added successfully.');
			}
			redirect($this->uri->segment(1).'/');
		}
	}// end save
  
 	function check_duplicate($skill_id)
    {
		
		$where = " skill_id  = '".(int)$skill_id."' ";
 		if(! $this->common_model->record_exist("Skill_Mapping", $where))
		{
			return true;	
		}
		else
		{
			$this->form_validation->set_message('check_duplicate', 'The %s Must be Unique.');
			return false;
		}
    }// end check_duplicate

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */